const { logger } = require('../utils');
const { knex, TABLE_NAME } = require('./db');

knex.schema.hasTable(TABLE_NAME.user).then((exists) => {
  if (!exists) {
    return knex.schema.createTable(TABLE_NAME.user, (t) => {
      t.charset('utf8');
      t.uuid('id').primary();
      t.string('userName', 100);
      t.string('userId', 50);
      t.string('openId', 50);
      t.string('mobile', 20);
      t.string('agentName', 100);
      t.string('agentMobile', 20);
      t.string('organizationId');
      t.foreign('organizationId').references(`${TABLE_NAME.organization}.id`)
        .onDelete('SET NULL');
      t.boolean('verified', 10);
      t.json('roles', 200);
      t.integer('quiteJob', 2);
      t.timestamp('createdOn').defaultTo(knex.fn.now());
      t.timestamp('verifiedOn').defaultTo(knex.fn.now());
    });
  }
})
  .catch((err) => {
    logger.error({
      message: err.message,
      stack: err.stack,
      Action: 'user/createTable',
    });
    console.error(err);
  });

